<template>
  <a-form :rules="districtRules" :model="districtFormConfig.form" @finish="submit" :label-col="{ span: 6 }"
    :wrapper-col="{ offset: 1 }" label-align="right" ref="districtFormRef" :disabled="hasPerm('ax:district:edit')">
    <FormCard title="编辑/新增" :back="back" subtitle="地区">
      <template #extra>
        <a-button htmlType="submit" type="primary" :loading="districtTable.loading" block>
          保存
        </a-button>
      </template>
      <a-row :gutter="8">
        <a-col :span="16">
          <a-card>
            <a-form-item label="自增id" name="districtId">
              <a-input allow-clear placeholder="请输入自增id" v-model:value="districtFormConfig.form.districtId">
              </a-input>
            </a-form-item>
            <a-form-item label="父级关系" name="pid">
              <a-input allow-clear placeholder="请输入父级关系" v-model:value="districtFormConfig.form.pid">
              </a-input>
            </a-form-item>
            <a-form-item label="地区名称" name="district">
              <a-input allow-clear placeholder="请输入地区名称" v-model:value="districtFormConfig.form.district">
              </a-input>
            </a-form-item>
            <a-form-item label="子属关系" name="level">
              <a-input allow-clear placeholder="请输入子属关系" v-model:value="districtFormConfig.form.level">
              </a-input>
            </a-form-item>
          </a-card>
        </a-col>
        <AxExtra></AxExtra>
      </a-row>
    </FormCard>
  </a-form>
</template>

<script setup lang="ts">
import {
} from '../data/options';
import { hasPerm } from '@/utils/common/perm';
import type { Response } from '@/api/config/types';
import {
  createDistrict,
  updateDistrict,
} from '@/api/modules/ax/district/district';
import { notify, } from '@/utils/table/table';
import { districtList } from '../data/curd';
import {
  districtFormConfig,
  districtRules,
  districtFormRef
} from '../data/form';
import { districtTable } from '../data/table';
import AxExtra from './AxExtra.vue';

const submit = async () => {
  districtTable.value.loading = true;
  let result: undefined | Response<any> = undefined
  if (districtFormConfig.value.form.districtId) {
    const { data } = await updateDistrict(districtFormConfig.value.form)
    result = data
  } else {
    const { data } = await createDistrict(districtFormConfig.value.form)
    result = data
  }
  notify(result)
  districtTable.value.loading = false;
  await districtList();
  back();

};

const back = () => {
  districtFormConfig.value.display = false;
  districtFormConfig.value.reset!();
};

</script>

<style lang="scss" scoped></style>
